package com.sskj.gcc.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.webkit.WebView;

import com.just.agentweb.AgentWebUIControllerImplBase;
import com.just.agentweb.DefaultUIController;
import com.just.agentweb.LogUtils;
import com.just.agentweb.WebParentLayout;

public class WebUIController extends AgentWebUIControllerImplBase {
    private Activity mActivity;

    @Override
    public void onSelectItemsPrompt(WebView view, String url, final String[] ways, final Handler.Callback callback) {
        showChooserInternal(ways, callback);
    }

    private void showChooserInternal(String[] ways, final Handler.Callback callback) {
        Activity mActivity;
        if ((mActivity = this.mActivity) == null || mActivity.isFinishing()) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (mActivity.isDestroyed()) {
                return;
            }
        }
        AlertDialog mAlertDialog = new AlertDialog.Builder(mActivity)
                .setSingleChoiceItems(ways, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        LogUtils.i(TAG, "which:" + which);
                        if (callback != null) {
                            Message mMessage = Message.obtain();
                            mMessage.what = which;
                            callback.handleMessage(mMessage);
                        }

                    }
                }).setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        dialog.dismiss();
                        if (callback != null) {
                            callback.handleMessage(Message.obtain(null, -1));
                        }
                    }
                }).create();
        mAlertDialog.show();
    }

    @Override
    protected void bindSupportWebParent(WebParentLayout webParentLayout, Activity activity) {
        this.mActivity = activity;

    }
}
